package com.siemens.industrial.web.mvc.controller.admin;

import com.siemens.industrial.model.admin.Metadata;
import com.siemens.industrial.model.admin.User;
import com.siemens.industrial.utils.GlobalResponseCode;
import com.siemens.industrial.utils.RestResponse;
import com.siemens.industrial.web.mvc.service.AuthService;
import com.siemens.industrial.web.mvc.service.admin.MetadataService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by qupen on 2016/10/10.
 */
@Controller
public class MetadataController {

    @Resource
    MetadataService metadataService;

    @Resource
    AuthService authService;

    @RequestMapping(value = "/admin/metadata/list", method = RequestMethod.GET)
    public String getPage() {
        return "admin/machineMetadata_manage";
    }

    @RequestMapping(value = "/admin/api/v1/metadata/addIDLE", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> createMetadataIDLE(@RequestBody ArrayList<Map<String,Object>> metadatas) {

        metadataService.batchSave(metadatas, Metadata.METAKEY_IDLE);

        return RestResponse.create(GlobalResponseCode.SUCCESS).build();
    }

    @RequestMapping(value = "/admin/api/v1/metadata/addFaultType", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> createMetadataFaultType(@RequestBody ArrayList<Map<String,Object>> metadatas) {

        metadataService.batchSave(metadatas, Metadata.METAKEY_TYPE);

        return RestResponse.create(GlobalResponseCode.SUCCESS).build();
    }

    @RequestMapping(value = "/admin/api/v1/metadata/MachineNumber", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> createMetadataMachineNumber(@RequestBody ArrayList<Object> metadatas) {

        metadataService.batchSaveMT(metadatas, Metadata.METAKEY_NUMBER);

        return RestResponse.create(GlobalResponseCode.SUCCESS).build();
    }

    @RequestMapping(value = "/admin/api/v1/metadata/metadatas", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getMetadatasByKey(String key) {

        List<Metadata> metadataList = metadataService.getAllMetadataBykey(key);

        return RestResponse.create(GlobalResponseCode.SUCCESS).putData(metadataList).build();
    }

    @RequestMapping(value = "/admin/api/v1/metadata/list", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getMetadatas() {

        List<Metadata> metadataList = metadataService.getAllMetadata();

        return RestResponse.create(GlobalResponseCode.SUCCESS).putData(metadataList).build();
    }

    @RequestMapping(value = "/admin/api/v1/metadata/delete", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> deleteMetadata(@RequestParam(name = "metadataId") String metadataId) {

        metadataService.deleteMetadata(metadataId);

        return RestResponse.create(GlobalResponseCode.SUCCESS).build();
    }

    @RequestMapping(value = "/admin/api/v1/metadata/one", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getMetadataByMetaId(String metaId) {

        Metadata metadata = metadataService.getMetadataByMetaId(metaId);

        return RestResponse.create(GlobalResponseCode.SUCCESS).putData(metadata).build();
    }

    @RequestMapping(value = "/admin/api/v1/metadata/updateByMetaId", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getOneById(@RequestBody Map<String,Object> map) {
        metadataService.updateById(map.get("definition"),map.get("metaId").toString());
        return RestResponse.create(GlobalResponseCode.SUCCESS).build();
    }
}
